//
//  ZBScrollView.m
//  MoBan
//
//  Created by apple on 16/7/9.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ZBScrollView.h"
#import "ZBIteamCell.h"
@interface ZBScrollView()
@property(nonatomic,strong)NSMutableArray *array;
/**
 一页显示的个数
 */
@property(nonatomic,assign)NSInteger pagecount;
@end

@implementation ZBScrollView
-(NSMutableArray *)array
{
    if (!_array) {
        _array = [NSMutableArray array];
        
    }
    return _array;
}
-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        [self addUI];
    }
    return self;
}
/**
 添加UI   思路：1.首先根据i判断是在第几页  2.其次再判断他是在上面还是下面
 */
-(void)addUI
{
    
    self.pagecount = 8;
    
    [self.array removeAllObjects];
    [self.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSMutableArray *array = [self.datasouce ZBAddIteamCellWithArray];
    
    for (NSInteger i=0; i<array.count; i++) {
        ZBIteamCell *iteamcell = [[ZBIteamCell alloc]init];
        iteamcell.tag = i;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(cellClic:)];
        [iteamcell addGestureRecognizer:tap];
        [self addSubview:iteamcell];
        [self.array addObject:iteamcell];
    }
    
        NSInteger page;
    
        if (array.count%self.pagecount==0) {
            page = array.count/self.pagecount;
        }else
        {
            page = array.count/self.pagecount+1;
        }

    self.contentSize= CGSizeMake(KscrW*page, self.height);
    
    
}
-(void)layoutSubviews
{
    [super layoutSubviews];
    
    for (NSInteger i=1; i<=self.array.count; i++) {
        
        NSInteger page;
        
        if (i%self.pagecount==0) {
            page=i/self.pagecount-1;
        }else
        {
            page=i/self.pagecount;
        }
        
        BOOL isUp;
        NSInteger uppage;
        
        if (i%(self.pagecount/2)==0) {
            uppage =i/(self.pagecount/2);
        }else
        {
            uppage = i/(self.pagecount/2)+1;
        }
        
        if (uppage%2==0) {
            isUp=NO;
        }else
        {
            isUp = YES;
        }
     
        
        ZBIteamCell *cell = self.array[i-1];
        //每一个cell的宽度
        CGFloat everyCellW =self.width/(self.pagecount/2);
        if (isUp) {
            
            //上面的第i个的x值
            NSInteger upI =i-1-self.pagecount*page;
            if (self.array.count>self.pagecount/2) {
                //这个地方加判断主要是要判断刚刚开始的时候是几行
                cell.frame = CGRectMake(KscrW*page+everyCellW*upI, 0, everyCellW,  self.height/2);
            }else
            {
                cell.frame = CGRectMake(KscrW*page+everyCellW*upI, 0, everyCellW,  self.height);

            }
            
        }else
        {
            //下面的第i个的x值
            NSInteger downI =i-1-page*self.pagecount -self.pagecount/2;
            
            cell.frame = CGRectMake(KscrW*page+everyCellW*downI, self.height/2 , everyCellW,  self.height/2);
            
        }
        
    }

    
    

}
-(void)reloadDta
{
    [self addUI];

}
-(void)ZBSetDatasouce:(NSString *)str
{

    
}
/**
 点击cell的方法
 */
-(void)cellClic:(UIGestureRecognizer *)tap
{
    ZBIteamCell *cell = (ZBIteamCell *)tap.view;
    
    if ([self.deleget respondsToSelector:@selector(ZBIteamCell:didSelected:)]) {
        [self.deleget ZBIteamCell:cell didSelected:tap.view.tag];
    }
}
@end
